/*
 * MutablePen.h
 *
 *  Created on: 2018年8月13日
 *      Author: zhengboyuan
 */

#ifndef MUTABLEPEN_H_
#define MUTABLEPEN_H_

#include "BasicType.h"


class MutablePen
{
public:
	MutablePen():
		m_hPen(),
		m_style(),
		m_width(),
		m_color()
	{
	}

	virtual ~MutablePen()
	{
		deleteObject();
	}

	HPEN get() const
	{
		return m_hPen;
	}

	operator HPEN() const { return m_hPen; }

	bool IsNull() const { return (m_hPen == NULL); }

	HPEN createPen(int nPenStyle, int nWidth, COLORREF crColor)
	{
		deleteObject();

		m_hPen = ::CreatePen(nPenStyle, nWidth, crColor);

		m_style = nPenStyle;
		m_width = nWidth;
		m_color = crColor;

		return m_hPen;
	}

	BOOL deleteObject()
	{
		if (m_hPen == NULL)
		{
			return true;
		}

		BOOL bRet = ::DeleteObject(m_hPen);
		if (bRet)
		{
			m_hPen = NULL;
		}
		return bRet;
	}

	HPEN update(int nPenStyle, int nWidth, COLORREF crColor)
	{
		if (m_hPen == NULL)
		{
			return createPen(nPenStyle, nWidth, crColor);
		}

		if (m_style != nPenStyle || m_width != nWidth || m_color != crColor)
		{
			return createPen(nPenStyle, nWidth, crColor);
		}
		return m_hPen;
	}

protected:
	HPEN m_hPen;
	int m_style;
	int m_width;
	COLORREF m_color;

};

#endif /* MUTABLEPEN_H_ */
